﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.Presenter.AddToExpediente;
using MS.InformationManagement.Support;
using MS.InformationManagement.Support.Constants;

namespace MS.InformationManagement.WP.Layouts.MS.InformationManagement.WP
{
    public partial class IncludeInRecord : BaseApplicationPage, IAddExpedienteView
    {

        #region fields

        AddToExpedientePresenter _presenter;

        #endregion

        #region Properties

        public string SelectedRadicadoNumber { get; set; }

        public string SelectedExpedienteId
        {
            get { return txtExpediente.Text.Trim(); }
        }

        public string SelectedRadicadoId
        {
            get { return Page.Request.QueryString["ItemsId"].Split('|')[0]; }
        }

        public DAL.DocumentCenter.Expediente ExpedienteEntity { get; set; }

        public string CurrentListName
        {
            get { return SPContext.Current.Web.CurrentUser.LoginName.GetUserWithOutDomain(); }
        }

        #endregion

        #region Form Events

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            txtExpediente.Required(ExceptionConstants.ERROR_CAMPO_NULO, "ValBtnAddRadicado", AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);            
            btnAceptar.Visible = false;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            _presenter = new AddToExpedientePresenter(this);
            _presenter.HandleLoad();
            lblMessage.Text = string.Format("Para anexar el radicado {0} a un expediente debe primero buscar el expediente y presionar el botón aceptar", SelectedRadicadoNumber);
        }

        protected void Action_Search(object sender, EventArgs e)
        {
            try
            {
                _presenter.SearchExpediente();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void Action_Ok(object sender, EventArgs e)
        {
            try
            {
                _presenter.SearchExpediente();
                _presenter.AddToExpediente();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void Action_Cancel(object sender, EventArgs e)
        {
            this.CloseWindow(true);
        }


        #endregion

        #region Implemented Methods

        public void ShowFoundedExpediente()
        {
            if (ExpedienteEntity == null)
            {
                HandleWarning("El criterio de Busqueda utilizado no corresponde a ningun expediente");
                lblAsuntoValue.Text = string.Empty;
                lblFechaCreacionValue.Text = string.Empty;
                btnAceptar.Visible = false;
            }
            else
            {
                btnAceptar.Visible = true;
                lblAsuntoValue.Text = ExpedienteEntity.Asunto;
                lblFechaCreacionValue.Text = ExpedienteEntity.FechaInicial.ToString();
            }
        }

        public void RadicadoAdded()
        {
            this.CloseWindow();
        }

        #endregion
        
    }
}
